package com.software.myopiaparents.utils;

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.os.Build;
import android.support.v4.app.NotificationCompat;

import com.software.myopiaparents.R;
import com.software.myopiaparents.content.Constant;

import timber.log.Timber;

import static android.content.Context.NOTIFICATION_SERVICE;

/**
 * Created by wuxin on 2018/7/18.
 */

public class NotificationUtils {

    public static int pushNum = 0;


    /**
     * 版本升级兼容7.0以上
     * @param context
     * @param id
     * @param title
     * @param content
     * @param pendingIntent
     */
    public static void notificationMsg(Context context, String id, String title, String content, PendingIntent pendingIntent) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationManager notificationManager = (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE);

            NotificationChannel channel = new NotificationChannel(id, id, NotificationManager.IMPORTANCE_HIGH);

            if (notificationManager != null) {
                notificationManager.createNotificationChannel(channel);
            }


            NotificationCompat.Builder builder = new NotificationCompat.Builder(context);


            builder.setContentTitle(title)//设置通知栏标题
                    .setContentIntent(pendingIntent) //设置通知栏点击意图
                    .setContentText(content)
                    .setNumber(pushNum)
                    .setTicker(content) //通知首次出现在通知栏，带上升动画效果的
                    .setWhen(System.currentTimeMillis())//通知产生的时间，会在通知信息里显示，一般是系统获取到的时间
                    .setSmallIcon(R.mipmap.ic_launcher)//设置通知小ICON
                    .setChannelId(id)
                    .setDefaults(Notification.DEFAULT_ALL);

            Notification notification = builder.build();
            notification.flags |= Notification.FLAG_AUTO_CANCEL;
            if (notificationManager != null) {
                notificationManager.notify((int) Long.parseLong(id), notification);
            }
        }
        Timber.tag(Constant.TAG).e("id=" + id);
    }

    public static void cancelNotificationMsg(Context context, int id) {
        NotificationManager notificationManager = (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE);
        if (notificationManager != null)
            notificationManager.cancel(id);
    }
}
